<template>
  <!-- 新增投资商资金 弹框 -->
  <el-dialog title="新增投资商资金" v-model="editVisible" width="30%" draggable>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="150px" v-loading="loading">
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="资金操作类型：" prop="type">
            <el-select v-model="form.type" placeholder="请选择资金操作类型">
              <el-option v-for="item in statusList" :key="item.id" :label="item.dictLabel" :value="item.dictValue"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="金额：" prop="money">
            <el-input v-model.trim="form.money" placeholder="请填写金额"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="19">
          <el-form-item label="操作时间：" prop="operation_time">
            <el-date-picker v-model="form.operation_time" type="date" format="YYYY/MM/DD" value-format="x" placeholder="选择操作时间"> </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="1">
          <el-tooltip placement="top" content="操作时间只需要在投资和转出类型的时候填写，描述的是投资商在支付宝账号取出或转入资金时的时间">
            <el-icon class="icon-wzg-config"><InfoFilled /></el-icon>
          </el-tooltip>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="20">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="disabledbtn" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { useDictData } from "@/hooks/network/useDictData";
import { useState } from "@/hooks/useState.ts";
import { isNumber } from "@/shared";
import type { IDictData } from "@/types/dict";
import type { IInvestorsMoneyLogInfoDTO } from "@/types/user/investorMoneyLogInfo";
import { postInvestorMoneyLogInfo } from "@/utils/api/investorMoneyLog";
import { safeAsync } from "@/utils/utils";
const { disabledbtn, loading, toggleState } = useState();
let { getDictInfoV2 } = useDictData();
const statusList = ref<Array<IDictData>>([]);

const emit = defineEmits<{
  handleSuccess: [];
}>();
const editVisible = defineModel<boolean>("editVisible", { required: true });
const form = defineModel<IInvestorsMoneyLogInfoDTO>("form", { required: true });
const formRef = useTemplateRef("formRef");
const rules = reactive({
  type: [{ required: true, message: "请选择资金操作类型", trigger: "blur" }],
  money: [{ required: true, message: "请填写金额", trigger: "blur" }]
});

watch(
  () => editVisible.value,
  newValue => {
    if (newValue) {
      /* 初始化数据 */
    } else {
      formRef.value?.resetFields();
      formRef.value?.clearValidate();
    }
  }
);
/* 数据校验 */
function handleVerifyData() {
  if (!isNumber(form.value.money)) {
    ElMessage.warning("请填写合法的金额");
    return false;
  }
  if (["invest", "transfor_out"].includes(form.value.type) && Number(form.value.money) <= 0) {
    ElMessage.warning("请填写大于0的金额");
    return false;
  }
  if (["invest", "transfor_out"].includes(form.value.type) && form.value.operation_time === "") {
    ElMessage.warning("请填写操作时间");
    return false;
  }
  return true;
}
/* 数据校验 */
function submitForm() {
  if (!formRef.value) return;
  formRef.value.validate(valid => {
    if (!valid) {
      ElMessage.warning("请填写必填字段");
      return;
    }
    if (!handleVerifyData()) return;
    submitFormInfo();
  });
}
/* 保存或编辑信息 */
function submitFormInfo() {
  toggleState(true);
  postInvestorMoneyLogInfo(form.value)
    .then(res => {
      ElMessage.success(res.msg);
      emit("handleSuccess");
      editVisible.value = false;
    })
    .catch(() => {})
    .finally(() => {
      toggleState(false);
    });
}

onMounted(async () => {
  let [err, list] = await safeAsync<Array<IDictData>, [string]>(getDictInfoV2)("INVESTOR_MONEY_LOG_TYPE");
  if (!err) {
    statusList.value = list!.filter(item => ["invest", "transfor_out", "reverse", "collection"].includes(item.dictValue));
  }
});
</script>

<style lang="scss" scoped></style>
